package kgame5.pbe.atlantic.comp {
	import kgame5.k3d.core.obj3d.spacial.SpacialVO;
	import kgame5.pbe.atlantic.event.ViewEvent;
	import kgame5.pbe.atlantic.vo.EntityVO;
	import kgame5.pbe.engine.PBE;
	import kgame5.pbe.engine.components.AnimatedComponent;

	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import kgame5.pbe.engine.components.TickedComponent;
	import kgame5.kutil.T;

	/**
	 * @author max
	 * 2010-12-13
	 * 只处理与显示相关事情
	 * 与整个外部交互不需要处理
	 */
	public class ViewComp2D extends AnimatedComponent{
		
		public static const NAME:String="ViewComp2D";
		
		//注意，vo在view模块是只读的
		protected var entVO:EntityVO;
		protected var spcVO:SpacialVO;
		
		//容器
		//mc_c的存在是很重要的，它保证在没加载到内容的时候，已经有个mc_c可以供外部使用了
		protected var mc_c:Sprite;
		
		private var layerID:int=0;//图层,这个放在构造函数里是为了防止忘记
		
		public function ViewComp2D(entVO:EntityVO,layerID:int){
			this.entVO=entVO;
			this.spcVO = entVO.spcVO;
			mc_c=new Sprite();
			this.layerID=layerID;
		}
		
		public function getDO():Sprite{
			return mc_c;
		}
		
		override public function onFrame(deltaTime:Number):void
        {
        	
		}

		override protected function onAdd():void{
			super.onAdd();
			PBE.viewManager.addNode(mc_c, layerID);
		}
		
		override protected function onRemove():void{
			super.onRemove();
			PBE.viewManager.removeNode(mc_c, layerID);
			
			this.unbindMouseEventSource();
		}
		
//		//注册事件
//		public function getDO():DisplayObject{
//			return mc_c;
//		}
		
		//====================接收外部命令===================
		//跳转到某个制定动画并播放
		public function playAnim(animID:String,frame:int=0):void{
			
		}
		
		//切换到另一种形态
		public function changeForm(formID:int):void{
			
		}
		
		//改变方向
		public function changeDir(dir:int):void{
			
		}
		
		//===================供方便子类使用=================
		//设置发出点击，鼠标滑进，滑出的事件源
		private var mc_evtSource:Sprite;
		
		protected function bindMouseEventSource(mc_evt:Sprite):void{
			mc_evtSource=mc_evt;
			mc_evtSource.addEventListener(MouseEvent.CLICK, _onClick);
			mc_evtSource.addEventListener(MouseEvent.ROLL_OVER, _onRollOver);
			mc_evtSource.addEventListener(MouseEvent.ROLL_OUT, _onRollOut);
		}
		
		protected function unbindMouseEventSource():void{
			if(mc_evtSource){
				mc_evtSource.removeEventListener(MouseEvent.CLICK, _onClick);
				mc_evtSource.removeEventListener(MouseEvent.ROLL_OVER, _onRollOver);
				mc_evtSource.removeEventListener(MouseEvent.ROLL_OUT, _onRollOut);
			}
		}
		
		private function _onClick(e:MouseEvent):void{
			sendEvent(ViewEvent.NAME_CLICK,{x:e.localX,y:e.localY});
			
		}
		private function _onRollOver(e:MouseEvent):void{
			sendEvent(ViewEvent.NAME_ROLL_OVER, null);
		}
		private function _onRollOut(e:MouseEvent):void{
			sendEvent(ViewEvent.NAME_ROLL_OUT,null);
		}
		
		
		
		
		//实用的两个函数
		
		protected function sendEndOfFrameEvent(animID:String):void{
			sendEvent(ViewEvent.NAME_EOF,animID);
		}
		
		protected function sendAssetOKEvent():void{
			sendEvent(ViewEvent.NAME_ASSET_OK);
		}
		
		protected function sendSizeChangedEvent(x:int,y:int,width:int,height:int):void{
			sendEvent(ViewEvent.NAME_SIZE_CHANGED,{x:x,y:y,width:width,height:height});
		}
		
		//对外发送事件
		public function sendEvent(name:String,data:Object=null):void{
			if(owner&&owner.eventDispatcher){
				owner.eventDispatcher.dispatchEvent(new ViewEvent(name,data));
			}else{
				T.p("!!!!!!!!!!!!!ViewComp2D.sendEvnet"+name+" "+entVO.id+" "+entVO.typeID+" !!有空的东西");
			}
		}
	}//end of class
}